package com.sky.controller.admin;

import com.sky.constant.MessageConstant;
import com.sky.result.Result;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import lombok.extern.slf4j.Slf4j;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;


import java.io.File;
import java.io.IOException;
import java.util.UUID;

/**
 * @program: sky-take-out
 * @description:
 * @author: lydms
 * @create: 2024-01-05 20:10
 **/

@RestController
@RequestMapping("/admin/common")
@Api(tags="通用接口")
@Slf4j
public class CommonController {


    /**
     * 文件上传
     * @param file
     * @return
     */
    @PostMapping("/upload")
    @ApiOperation("文件上传")
    //TODO oss文件上传
    public Result<String> upload(MultipartFile file) {

        log.info("文件上传：{}",file);
        String originFilename = file.getOriginalFilename();
        String newFileName = UUID.randomUUID().toString()+originFilename.substring(originFilename.lastIndexOf("."));
        try {
            file.transferTo(new File("D:/桌面/image/"+newFileName));
            return Result.success("D:/桌面/image/"+newFileName);
        } catch (IOException e) {
            log.info("{}",e);
        }
        return Result.error(MessageConstant.UPLOAD_FAILED);
    }
}
